﻿using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//瓦片
public class GameTile : MonoBehaviour
{
   [SerializeField]
   Transform arrow = default;

    //跟踪瓦片周围的四个相邻瓦片
    GameTile north, east, south, west;
    //下一个位置
    GameTile nextOnPath;
    int distance;
    
    //敌人在瓦片上的出口位置
    public Vector3 ExitPoint{ get; private set;}
    
    //瓦片类型
    public GameTileContentType TileType = default;

    //路径属性
    public Direction PathDirection{ get; private set;}

    GameTileContent content;
    public GameTileContent Content{
        get => content;
        set{
            Debug.Assert(value != null, "Null assigened to content!");
            //回收以前的内容，并定位新内容
            if(content != null){
                content.Recycle();
            }
            content = value;
            content.transform.localPosition = transform.localPosition;
        }
    }

    //建立东西瓦片邻居关系
    public static void MakeEastWestNeighbors(GameTile east, GameTile west)
    {
        Debug.Assert(west.east == null && east.west == null, "Redefined neighbors!");//如果第一个参数为flase，则使用第二个参数消息记录断言错误

        west.east = east;
        east.west = west;
    }
    //建立南北邻居关系
    public static void MakeNorthSouthNeighbors(GameTile north, GameTile south)
    {
        Debug.Assert(south.north == null && north.south == null, "Redefined neighbors!");

        south.north = north;
        north.south = south;
    }

    //初始化路径数据
    public void ClearPath()
    {
        distance = int.MaxValue;
        nextOnPath = null;
    }
    //最终的目的瓦片
    public void BecomeDestination()
    {
        distance = 0;
        nextOnPath = null;
        //目的地瓦片的出口点设置为其中心
        ExitPoint = transform.localPosition;
    }
    //检查当前tile是否有路径
    public bool HasPath => distance != int.MaxValue;

    GameTile GrowPathTo(GameTile neighbor, Direction direction)
    {
        Debug.Assert(HasPath, "No path!");

        if(neighbor == null || neighbor.HasPath)
            return null;//如果一个邻居已经有了路径或则邻居为空，就可以中止了

        neighbor.distance = distance + 1;
        neighbor.nextOnPath = this;

        //添加一个半向量将出口点设置到瓦片边缘
        neighbor.ExitPoint = neighbor.transform.localPosition + direction.GetHalfVector();
        neighbor.PathDirection = direction;
        return neighbor.content.BlocksPath ? null : neighbor;
    }

    //沿指定方向扩展路径
    public GameTile GrowPathNorth() => GrowPathTo(north, Direction.South);
    public GameTile GrowPathEast() => GrowPathTo(east, Direction.West);
    public GameTile GrowPathSouth() => GrowPathTo(south, Direction.North);
    public GameTile GrowPathWest() => GrowPathTo(west, Direction.East);

    //旋转
    static Quaternion //四元数
        northRotation = Quaternion.Euler(90f, 0f, 0f),//返回一个围绕 Z 轴旋转 z 度、围绕 X 轴旋转 x 度、围绕 Y 轴旋转 y 度的旋转。
        eastRotation = Quaternion.Euler(90f, 90f, 0f),
        southRotation = Quaternion.Euler(90f, 180f, 0f),
        westRotation = Quaternion.Euler(90f, 270f, 0f);

    //判断旋转箭头
    public void ShowPath()
    {
        if(distance == 0){
            arrow.gameObject.SetActive(false);
            return;
        }

        arrow.gameObject.SetActive(true);
        arrow.localRotation = 
            nextOnPath == north ? northRotation : 
            nextOnPath == east ? eastRotation :
            nextOnPath == south ? southRotation :
            westRotation;
    }

    //是否是一个可选的tile
    public bool IsAlternative{get;set;}

    //设置箭头隐藏
    public void HidePath(){
        arrow.gameObject.SetActive(false);
    }

    //获取路径上的下一个瓦片
    public GameTile NextTileOnPath => nextOnPath;

}
